﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;

namespace EnterpriseFund.Controllers
{
        [OutputCache(Duration = 0, VaryByParam = "none")]
    public class AllowanceIssueManageController : AdminBaseController
    {
        //
        // GET: /AllowanceIssueManage/
        [AuthorityActionFilter("m_AllowanceIssueManage", Order = 1)]
        [LogPageActionFilter("期号管理", Order = 2)]
        public ActionResult Index()
        {
            BLL.AllowanceIssue codeNameBll = new BLL.AllowanceIssue();
            List<Model.AllowanceIssue> codeNameList = new List<Model.AllowanceIssue>();
            codeNameList = codeNameBll.GetModelList("");
            BLL.AllowanceClass allowanceClassBLL=new BLL.AllowanceClass();
            ViewData["AllowanceClassList"] = allowanceClassBLL.GetModelList("");
            return View(codeNameList);
        }

        public ActionResult AllowanceIssueListPartial()
        {
            string strwhere = "1=1";
            string FYear = Request.Params["FYear"];
            string FAllowanceClassNo = Request.Params["FAllowanceClassNo"];

            if (FYear != "")
            {
                strwhere += " and FYear=" + FYear;
            }
            if (FAllowanceClassNo != "")
            {
                strwhere += " and a.FAllowanceClassNo='" + FAllowanceClassNo + "'";
            }

            BLL.AllowanceIssue codeNameBll = new BLL.AllowanceIssue();
            List<Model.AllowanceIssue> codeNameList = new List<Model.AllowanceIssue>();
            codeNameList = codeNameBll.GetModelList(strwhere);
            ViewData["blcode"] = true;
            return PartialView("AllowanceIssueListPartial", codeNameList);
        }

        public ActionResult Save(Model.AllowanceIssue a)
        {
            try
            {
                a.FInsDate = DateTime.Now;
                a.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                a.FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                a.FInsIP = Common.GenerateUtil.GetClientIPv4Address();
                a.FIsReturn = "N";
                a.FStatus = "0";
                BLL.AllowanceIssue aBLL = new BLL.AllowanceIssue();
                aBLL.Add(a);
                ViewData["jsAlert"] = "操作成功";
            }
            catch(Exception ex)
            {
                ViewData["jsAlert"] = ex.Message;
            }
            BLL.AllowanceClass allowanceClassBLL = new BLL.AllowanceClass();
            ViewData["AllowanceClassList"] = allowanceClassBLL.GetModelList("");
            return View("AllowanceIssueEdit", a);
        }

        public string GetFIssueNo(string year, string allowanceClassNo)
        {
            try
            {
                BLL.AllowanceIssue a = new BLL.AllowanceIssue();
                string m = a.GetMaxNo(allowanceClassNo, year);
                if (m != "")
                {
                    int i = int.Parse(m.Substring(m.Length - 3));
                    return allowanceClassNo + year.Substring(year.Length - 2) + (i + 1).ToString("000");
                }
                else
                {
                    return allowanceClassNo + year.Substring(year.Length - 2) + "001";
                }
            }
            catch(Exception ex)
            {
                return "ex";
            }
        }

        public string DelAllowanceIssue(string issueNo)
        {
            string str = string.Empty;
            if (null != issueNo)
            {
                issueNo = Fetch.estimateString(issueNo, CheckGetEnum.Safety);
                BLL.AllowanceIssue codeNameBll = new BLL.AllowanceIssue();
                BLL.AllowanceIssueEntry allowanceIssueEntryBLL = new BLL.AllowanceIssueEntry();
                try
                {
                    if (allowanceIssueEntryBLL.GetModelList(" FIssueID='" + issueNo + "'").Count > 0)
                    {
                        str = "count";
                    }
                    else
                    {
                        codeNameBll.Delete(issueNo);
                        str = "ok";
                    }

                }
                catch (Exception)
                {
                    str = "error";
                }
            }
            return str;
        }

        public ActionResult AllowanceIssueEdit()
        {
            Model.AllowanceIssue a = new Model.AllowanceIssue();
            a.FYear = DateTime.Now.Year;
            a.FAllowanceClassNo = "J";
            a.FIssueNo = GetFIssueNo(a.FYear.ToString(), a.FAllowanceClassNo);
            BLL.AllowanceClass allowanceClassBLL = new BLL.AllowanceClass();
            ViewData["AllowanceClassList"] = allowanceClassBLL.GetModelList("");
            return View(a);
        }

    }
}
